﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapter24.Examples
{
    using System.Xml.Linq;

    public class Code08
    {
        public static void BeginEx04()
        {
            var xdoc = new XDocument(
                new XElement("customers",
                    new XElement("customer",
                        new XAttribute("ID", "A"),
                        new XAttribute("City", "New York"),
                        new XAttribute("Region", "North America"),
                        new XElement("order",
                            new XAttribute("Item", "Widget"),
                            new XAttribute("Price", 100)
                        ),
                        new XElement("order",
                            new XAttribute("Item", "Tire"),
                            new XAttribute("Price", 200)
                        )
                    ),
                    new XElement("customer",
                        new XAttribute("ID", "B"),
                        new XAttribute("City", "Mumbai"),
                        new XAttribute("Region", "Asia"),
                        new XElement("order",
                            new XAttribute("Item", "Oven"),
                            new XAttribute("Price", 501)
                        )
                    )
                )
            );
            var xmlFileName = @".\example2.xml";
            xdoc.Save(xmlFileName);
            var xdoc2 = XDocument.Load(xmlFileName);
            Console.WriteLine("Contents of xdoc2:");
            Console.WriteLine(xdoc2);
            Console.WriteLine("Press Enter/Return to continue...");
            Console.ReadLine();
        }

        public static void Begin()
        {
            var value =
@"<customers>
    <customer ID=""A"" City=""New York"" Region=""North America"">
        <order Item=""Widget"" Price=""100"" />
        <order Item=""Tire"" Price=""200"" />
    </customer>
    <customer ID=""B"" City=""Mumbai"" Region=""Asia"">
        <order Item=""Oven"" Price=""501"" />
    </customer>
</customers>";
            var xdoc = XDocument.Parse(value);
            Console.WriteLine(xdoc);
            Console.WriteLine("Press Enter/Return to continue...");
            Console.ReadLine();
        }
    }
}
